import com.NoteDao.NoteMapper;
import com.jiazhong.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class NoteMapperDao {

    //链接
    private static SqlSession sqlSession;
    static {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("Mybatis_comfig.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        //从配置文件中获取一个工厂对象
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);

        //通过工厂获取sqlsession对象
        sqlSession = factory.openSession();

    }
    //日志
    public static void rizhi() throws IOException {
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }

    public static void selectbyid() throws IOException {
        rizhi();
        NoteMapper mapper = sqlSession.getMapper(NoteMapper.class);
        Student userById = mapper.getUserById();
        System.out.println("userById = " + userById);
    }
}
